package com.andy.customerService.controller;

import com.andy.customerService.entity.User;
import com.andy.customerService.pojo.request.UserRequestParam;
import com.andy.customerService.service.UserService;
import com.andy.customerService.vo.JsonBaseResult;
import com.andy.customerService.vo.JsonPageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Andy
 * @since 2021-12-24
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     *
     * @param user
     */
    @PostMapping
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }

    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    public JsonBaseResult<User> queryUserById(@PathVariable("id") Long id,String flag) {
        System.out.println("flag = " + flag);
        return userService.queryUserById(id);
    }

    /**
     * 根据id修改用户
     */
    @PutMapping
    public void editUserById(@RequestBody User user) {
        userService.editUserById(user);
    }

    /**
     * 根据id删除用户
     * @param id
     */
    @DeleteMapping
    public void deleteUserById(@RequestParam(value = "id") Long id){
        userService.deleteUserById(id);
    }

    /**
     * 根据条件查询用户列表
     * @param requestParam
     */
    @PostMapping("/list")
    public JsonPageResult<Object> queryUserListByParam(@Valid @RequestBody UserRequestParam requestParam){
        return userService.queryUserListByParam(requestParam);
    }

}

